
# 用来输出两个字符串
def self_print(str1, str2, func_name=None):
    assert type(str1) != 'string', '请传入字符串'
    assert type(str2) != 'string', '请传入字符串'
    print(f'{func_name} 修改前{str1}')
    print(f'{func_name} 修改后{str2}')


# 字符串创建或者添加操作
def create():
    str_a, str_b = 'a', 'b'
    str_a += 'c'
    print(f'now_str_a is {str_a}')
    str_sum = str_a + str_b
    print(f'now_str_num is {str_sum}')
    return str_sum


# 字符串查找操作
def retrieve():
    need_retrieve_str = 'nljakswinmssk'
    # find 单个字符查找
    print(f'find 单个字符查找 index is {need_retrieve_str.find("s")}')
    # index 单个字符查找
    print(f'index 单个字符查找 index is {need_retrieve_str.index("s")}')
    # find 多个字符查找
    print(f'find 多个字符查找 index is {need_retrieve_str.find("win")}')
    # index 多个字符查找
    print(f'index 多个字符查找 index is {need_retrieve_str.find("win")}')
    # find 查找不存在的字符
    print(f'find 查找不存在的字符 index is {need_retrieve_str.find("hello")}')
    # index 查找不存在的字符
    try:
        print(f'index 查找不存在的字符 index is {need_retrieve_str.index("hello")}')
    except (RuntimeError, TypeError, NameError, ValueError):
        print('你需要查找的字符串不存在当前变量内')
    # startswith endswith
    print(f'以nlj开头吗? {need_retrieve_str.startswith("nlj")}')
    print(f'以ssk结尾吗？{need_retrieve_str.endswith("ssk")}')
    # 索引
    print(f'all string {need_retrieve_str[:]}  old id:{id(need_retrieve_str)} new id: {id(need_retrieve_str[:])}')
    print(f'从开头到某个下标: {need_retrieve_str[:4]}')
    print(f'区间: {need_retrieve_str[3: 7]}')
    # 右闭
    return need_retrieve_str


# 字符串更新修改操作
def update():
    need_update_str = 'abyss'
    # replace 修改 需要重新复制，字符串是不可变的，这个方法只会生成新的字符串
    self_print(need_update_str, need_update_str.replace('a', 's'), func_name='replace')
    # split 分割
    need_update_str = need_update_str[:2] + ' ' + need_update_str[2:]
    split_str = need_update_str.split(" ")
    self_print(need_update_str, split_str, func_name='split')
    # join合并
    join_str = " ".join(split_str)
    self_print(split_str, join_str, func_name="join")
    return need_update_str


# 字符串删除操作
def delete():
    need_delete_str = ' abyss '
    self_print(need_delete_str, need_delete_str.strip(), func_name='去除两端空格')
    self_print(need_delete_str, need_delete_str.strip(' as '), '指定删除两端')
    return need_delete_str


# 字符串格式化
def format_str():
    city = 'guangdong'
    print(f'{city}-abyss')
    print('{city}-abyss'.format(city="dongguan"))
    print('{0}-abyss'.format("dongguan"))
    return None


# write file
def write_current_file():
    f = open('./第二周_第二节.py', "w", encoding="utf-8")
    current_file = open('./第二周_第二节tmp.py', "r", encoding="utf-8")
    f.write(current_file.read())
    f.close()
    current_file.close()
    return None


if __name__ == '__main__':
    create()
    retrieve()
    update()
    delete()
    format_str()
    write_current_file()
